package com.guage.producter.controller;

import java.text.ParseException;
import java.util.List;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.guage.producter.domain.Student;
import com.guage.producter.service.StudentService;
import com.guage.producter.utils.Result;
import com.guage.producter.utils.SnowflakeIdWorker;
import com.pad.dpscn.daag.sdk.core.annotation.DataSendPosition;
@RestController
@RequestMapping("/student")
@DataSendPosition
public class StudentController {

	
	@Autowired
    private StudentService studentService;

	@Autowired
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	
	@PostMapping("/save")
	public Result<Boolean> insert(@RequestBody Student student) throws ParseException {
		SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
		long id = idWorker.nextId();
		java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
		student.setId(id+"");
		student.setRxsj(sqlDate);
		studentService.insert(student);
		return Result.success();
	}

	@DeleteMapping("/delete/{id}")
	public Result<Integer> delete(@PathVariable("id") Integer id) {
		int i = studentService.deleteStu(id);
		if (i > 0) {
			return Result.success("200", "成功删除一名学生", i);
		} else {
			return Result.error("400", "未能成功删除，请重新删除");
		}
	}

	@PutMapping("/update")
	public Result<Integer> update(Student student) {
		int i = studentService.updateStu(student);
		if (i > 0) {
			return Result.success("200", "成功更新一名学生", i);
		} else {
			return Result.error("400", "未能成功更新，请重新更新");
		}
	}

	@GetMapping("/byPage")
	public Result<PageInfo> getAllByPage(Student student, Integer pageNum, Integer pageSize) {
		PageInfo<Student> byPage = studentService.findByPage(student, pageNum, pageSize);
		return Result.success(byPage);
	}

	@GetMapping("/get")
	public Result<List> getAll() {
		return Result.success(studentService.findAll());
	}

	@GetMapping("/getOne")
	public Result<Student> selectOne(Integer id) {
		return Result.success(studentService.findById(id));
	}
}
